package org.whb.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.whb.basic.util.AjaxResult;
import org.whb.basic.util.LoginContext;
import org.whb.basic.util.PageList;
import org.whb.order.domain.AdoptOrder;
import org.whb.order.domain.OrderAddress;
import org.whb.order.query.AdoptOrderQuery;
import org.whb.order.service.IAdoptOrderService;
import org.whb.order.service.IOrderAddressService;
import org.whb.pet.domain.Pet;
import org.whb.pet.service.IPetService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {

    @Autowired
    private IAdoptOrderService adoptOrderService;

    @Autowired
    private IPetService petService;

    @Autowired
    private IOrderAddressService orderAddressService;

    /**
     * 宠物领养订单
     * @param params
     * @param request
     * @return
     */
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){
        try{
            String payData = adoptOrderService.submit(params, LoginContext.getLogininfo(request));
            System.out.println(payData);
            return AjaxResult.me().setResultObj(payData);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下单失败！"+e.getMessage());
        }

    }

    @PostMapping("/admin")
    public PageList<AdoptOrder> admin(@RequestBody AdoptOrderQuery query){
        return adoptOrderService.queryPage(query);
    }

    @GetMapping("{id}")
    public AdoptOrder getOrderDetail(@PathVariable Long id){
        return adoptOrderService.queryById(id);
    }

    /**
     * 获取收货人地址相关信息
     * @param unionPaySn
     * @return
     */
    @GetMapping("/getOrderAdress/{unionPaySn}")
    public OrderAddress getOrderAdress(@PathVariable String unionPaySn){
        return orderAddressService.getOrderAdress(unionPaySn);
    }
}
